package com.li.network.lab_4;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.CountDownLatch;

public class ExchangeThread extends Thread{
    private InputStream input;
    private OutputStream output;
    private CountDownLatch latch;

    public ExchangeThread(InputStream input, OutputStream output, CountDownLatch latch) {
        this.input = input;
        this.output = output;
        this.latch = latch;
    }

    @Override
    public void run() {
        try {
            //交换报文内容
            BufferedInputStream bis = new BufferedInputStream(input);
            byte[] buffer = new byte[1024];
            int length = -1;
            while ((length = bis.read(buffer)) != -1) {
                output.write(buffer, 0, length);
                output.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            latch.countDown();
            IOUtils.close(input, output);
        }
    }
}
